/**
 * 
 */
package com.xintoucloud.gsv.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.User;
import com.xintoucloud.gsv.mapper.DeptMapper;
import com.xintoucloud.gsv.model.CheckArr;
import com.xintoucloud.gsv.model.DeptData;
import com.xintoucloud.gsv.model.DeptNode;
import com.xintoucloud.gsv.model.DeptVo;
import com.xintoucloud.gsv.model.Status;
import com.xintoucloud.gsv.model.TreeData;
import com.xintoucloud.gsv.model.TreeNode;
import com.xintoucloud.gsv.service.DeptService;

/**
 * @author xiang
 *
 */
@Service@Transactional
public class DeptServiceImpl implements DeptService{
	@Autowired
	private DeptMapper deptMapper;
	
	//查询所有部门
	public List<Dept> listDept(){
		List<Dept> depts = deptMapper.listDept();
		return depts;
	}
	
	@Override
	public List<DeptNode> listDeptByParentId(Integer parentId) {
		List<Dept> depts=null;
		if(parentId==0) {
			//查找所有父节点为0的
			depts=deptMapper.listDeptIsZeroParentId();
		}else {
			depts=deptMapper.listDeptByParentId(parentId);
		}
		List<DeptNode> deptNodes=new ArrayList<>();
		for (Dept dept : depts) {
			DeptNode deptNode=new DeptNode();
			deptNode.setName(dept.getDname());
			deptNode.setId(dept.getDid());
			deptNode.setEalias(dept.getUser().getRealName());
			List<DeptNode> childs=listDeptByParentId(dept.getDid());
			deptNode.setChildren(childs);
			if(childs!=null) {
				deptNodes.add(deptNode);
				deptNode.setSpread(true);
			}else {
				deptNode.setSpread(false);
			}
		}
		return deptNodes;
	}

	@Override
	public Dept getOne(Integer did) {
		
		return deptMapper.getOne(did);
	}

	@Override
	public TreeData getTreeData(Integer parentId) {
		TreeData treeData = new TreeData();
		Status status=new Status();
		List<TreeNode> treeNodes=listTreeNode(parentId);
		if(treeNodes!=null) {
			status.setCode(200);
			status.setMessage("加载成功");
		}
			treeData.setData(treeNodes);
			treeData.setStatus(status);
		return treeData;
	}

	@Override
	public List<TreeNode> listTreeNode(Integer parentId) {
		List<Dept> depts=null;
		if(parentId==0) {
			//查找所有父节点为0的
			depts=deptMapper.listDeptIsZeroParentId();
		}else {
			depts=deptMapper.listDeptByParentId(parentId);
		}
		List<TreeNode> treeNodes=new ArrayList<>();
		for (Dept dept : depts) {
			TreeNode treeNode=new TreeNode();
			treeNode.setId(dept.getDid());
			treeNode.setTitle(dept.getDname());
			treeNode.setParentId(dept.getParentId());
			List<CheckArr> checkArrs=new ArrayList<>();
			CheckArr checkArr=new CheckArr();
			checkArr.setIsChecked("0");
			checkArr.setType("0");
			checkArrs.add(checkArr);
			treeNode.setCheckArr(checkArrs);
			List<TreeNode> childs=listTreeNode(dept.getDid());
			treeNode.setChildren(childs);
			if(childs.isEmpty()) {
				treeNode.setisLast(true);
			}else {
				treeNode.setisLast(false);
			}
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}

	@Override
	public Dept saveDept(int did, String dname) {
		System.out.println("did:="+did);
		System.out.println("dname:="+dname);
		Dept dept = new Dept();
		dept.setParentId(did);
		dept.setDname(dname);
		Dept dept_parentId = deptMapper.getOne(did);
		System.out.println("xxxxxxxxxxxxxxdname:="+dept_parentId.getUser());
		User user = new User();
			
		user.setUid(dept_parentId.getUser().getUid());
		dept.setUser(user);
		deptMapper.saveDept(dept);
		
		return dept;
	}

	@Override
	public boolean deleteDeptById(int did) {
		// 先查询其所有子节点
				boolean result=false;
				List<Dept> depts=deptMapper.listDeptByParentId(did);
				//如果没有子节点。执行删除操作
				if(depts.isEmpty()) {
					deptMapper.deleteDept(did);
					return depts.isEmpty();
				}else {
				//如果有子节点,遍历子节点集合，利用递归查寻有没有下一级的节点，如果有则继续遍历
				for (Dept dept : depts) {
					result=deleteDeptById(dept.getDid());
					}
				deptMapper.deleteDept(did);
				}
				return result;
	}

	@Override
	public void updateDept(int did, String dname) {
		deptMapper.updateOrganization(did, dname);	
	}

	@Override
	public List<Dept> listDeptWithParentIdIsFirst() {
		List<Dept> depts = deptMapper.listDeptByParentId(34);
		return depts;
	}

	@Override
	public DeptData listDeptByParentId1(Integer parentId) {
		DeptData deptData=new DeptData();
		List<Dept> depts = deptMapper.listDeptByParentId(parentId);
		List<DeptVo> deptVos=new ArrayList<>();
		for (Dept dept : depts) {
			DeptVo deptVo=new DeptVo();
			deptVo.setDid(dept.getDid());
			deptVo.setDname(dept.getDname());
			deptVo.setParentId(dept.getParentId());
			deptVos.add(deptVo);
		}
		deptData.setList(deptVos);
		if(deptVos!=null) {
			deptData.setMessage("成功");
			deptData.setStatus(200);
		}
		return deptData;
	}


}
